﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TracNghiemTinHocDaiCuong.BIZ;
using TracNghiemTinHocDaiCuong.Common;
using TracNghiemTinHocDaiCuong.ThucThe;

namespace TracNghiemTinHocDaiCuong
{
    public partial class QuanLyDeThi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LayDanhSach();
        }

        protected void btnThemDeThi_Click(object sender, EventArgs e)
        {
            Response.Redirect("ThemDeThi.aspx");
        }

        protected void btnSuaDeThi_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("SuaDeThi.aspx?ID={0}", ViewState["idDeThi"]));
        }

        protected void btnXoaDeThi_Click(object sender, EventArgs e)
        {
            DeThiBIZ.Xoa(Methods.ConvertNumber(ViewState["idDeThi"].ToString()));
            LayDanhSach();
        }

        protected void lkbtnDeThi_Click(object sender, CommandEventArgs e)
        {
            ViewState["idDeThi"] = Methods.ConvertNumber(e.CommandName);
        }

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            IList<DeThi> danhSachDeThi = DeThiBIZ.TimKiem(txtNoiDung.Text, hdfFilter.Value);
            BindDuLieu_HienThiThongBao(danhSachDeThi);
        }

        protected void btnXoaTuKhoa_Click(object sender, EventArgs e)
        {
            txtNoiDung.Text = "";
        }

        protected void ClickFilter(object sender, CommandEventArgs e)
        {
            hdfFilter.Value = e.CommandName;
            LocDanhSachDeThi(e.CommandName);
        }

        private void LocDanhSachDeThi(string phamVi)
        {
            IList<DeThi> danhSachDeThi = new List<DeThi>();
            switch (phamVi)
            {
                case "All":
                    {
                        danhSachDeThi = DeThiBIZ.LayDanhSach();
                        break;
                    }
                case "09":
                    {
                        danhSachDeThi = DeThiBIZ.TimKiem(txtNoiDung.Text,
                            "[0-9]%");
                        break;
                    }
                case "AE":
                    {
                        danhSachDeThi = DeThiBIZ.TimKiem(txtNoiDung.Text,
                            "[A-E]%");
                        break;
                    }
                case "FJ":
                    {
                        danhSachDeThi = DeThiBIZ.TimKiem(txtNoiDung.Text,
                            "[F-J]%");
                        break;
                    }
                case "KN":
                    {
                        danhSachDeThi = DeThiBIZ.TimKiem(txtNoiDung.Text,
                            "[K-N]%");
                        break;
                    }
                case "OR":
                    {
                        danhSachDeThi = DeThiBIZ.TimKiem(txtNoiDung.Text,
                            "[O-R]%");
                        break;
                    }
                case "SV":
                    {
                        danhSachDeThi = DeThiBIZ.TimKiem(txtNoiDung.Text,
                            "[S-V]%");
                        break;
                    }
                case "WZ":
                    {
                        danhSachDeThi = DeThiBIZ.TimKiem(txtNoiDung.Text,
                            "[W-Z]%");
                        break;
                    }
            }
            BindDuLieu_HienThiThongBao(danhSachDeThi);
        }
        

        private void BindDuLieu_HienThiThongBao(IList<DeThi> danhSachDeThi)
        {
            if (danhSachDeThi.Count == 0)
                lblThongBao.Text = "Chưa có bản ghi để hiển thị";
            else
                lblThongBao.Text = "";
            gvDanhSachDeThi.DataSource = danhSachDeThi;
            gvDanhSachDeThi.DataBind();
        }

        private void LayDanhSach()
        {
            IList<DeThi> deThi = DeThiBIZ.LayDanhSach();
            if (deThi.Count == 0)
                lblThongBao.Text = "Chưa có bản ghi để hiển thị"; 
            else
                lblThongBao.Text = "";
            gvDanhSachDeThi.DataSource = deThi;
            gvDanhSachDeThi.DataBind();
        }
    }
}